package com.chy.blog_web.modular.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_web.modular.web.entity.BoPhotoAlbum;
import com.chy.blog_web.modular.web.entity.BoPraiseHistory;
import com.chy.blog_web.modular.web.mapper.BoPhotoAlbumMapper;
import com.chy.blog_web.modular.web.service.IBoPhotoAlbumService;
import com.chy.blog_web.modular.web.service.IBoPraiseHistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：相册表 service业务层
 * 作者：chenhuiyan
 * 时间：2020/02/25 12:03
 */
@Service
@Transactional(readOnly = true)
public class BoPhotoAlbumServiceImpl extends ServiceImpl<BoPhotoAlbumMapper, BoPhotoAlbum> implements IBoPhotoAlbumService {

    @Autowired
    IBoPraiseHistoryService boPraiseHistoryService;

    /**
     * 查询列表
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> list(Map<String, Object> params){
        return baseMapper.list(params);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getInfoById(String id){
        return baseMapper.getInfoById(id);
    }

    /**
     * 相册点赞
     * @param id
     * @param ipAddress 用户IP地址
     */
    @Override
    @Transactional(readOnly = false)
    public boolean updatePraiseNum(String id, String ipAddress){

        boolean flag = false;

        //先判断该IP今天是否点过赞
        Calendar calendar = Calendar.getInstance();
        String year = calendar.get(Calendar.YEAR) + "";
        String month = calendar.get(Calendar.MONTH) < 10? "0" + (calendar.get(Calendar.MONTH) + 1) : (calendar.get(Calendar.MONTH) + 1) + "";
        String day = calendar.get(Calendar.DATE) < 10? "0" + calendar.get(Calendar.DATE) : calendar.get(Calendar.DATE) + "";
        String date = year + "-" + month + "-" + day;
        BoPraiseHistory boPraiseHistory = boPraiseHistoryService.getOne(new QueryWrapper<BoPraiseHistory>()
                .eq("ip_address", ipAddress)
                .eq("service_id", id).like("time", date)
                .eq("type", 2));

        if(null == boPraiseHistory){
            BoPhotoAlbum boPhotoAlbum = baseMapper.selectById(id);
            boPhotoAlbum.setPraiseNum(boPhotoAlbum.getPraiseNum() + 1);

            //点赞记录
            boPraiseHistory = new BoPraiseHistory();
            boPraiseHistory.preInsert();
            boPraiseHistory.setServiceId(boPhotoAlbum.getId());
            boPraiseHistory.setType(2);
            boPraiseHistory.setTime(new Date());
            boPraiseHistory.setIpAddress(ipAddress);

            baseMapper.updateById(boPhotoAlbum); //更新点赞数量

            boPraiseHistoryService.save(boPraiseHistory); //保存点赞记录

            flag = true;
        }
        return flag;
    }

}
